package sanrenxing.dayEnd.backRepertoryTotal.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sanrenxing.dayEnd.backRepertoryTotal.dao.DepositbusinessDao;
import sanrenxing.dayEnd.backRepertoryTotal.dao.FundAllocationDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Depositbusiness;
import sanrenxing.dayEnd.backRepertoryTotal.domain.FundAllocation;
import sanrenxing.dayEnd.backRepertoryTotal.service.DepositbusinessService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @desrciption 存款业务统计
 * @auth：张港峰
 * @date:2021/3/13 0013 - 9:19
 * @version:1:0
 */
@Service("DepositbuinessService")
public class DepositbusinessServiceImpl implements DepositbusinessService {

    //自动装配
    @Autowired
    private DepositbusinessDao depositbusinessDao;

    //注入资金调拨方法
    @Autowired
    FundAllocationDao fundAllocationDao;
    /**
     * 新增一行数据
     *
     * @param depositbusiness 对象
     * @return 0、增加失败 1、增加成功
     */
    @Override
    public int add(Depositbusiness depositbusiness) {
        return depositbusinessDao.add(depositbusiness);
    }

    /**
     * 删除一行数据
     *
     * @param DeposiId 编号
     * @return 0、删除失败 1、删除成功
     */
    @Override
    public int delete(int DeposiId) {
        return depositbusinessDao.delete(DeposiId);
    }

    /**
     * 修改一行数据
     *
     * @param depositbusiness 对象
     * @return 0、修改失败 1、修改成功
     */
    @Override
    public int update(Depositbusiness depositbusiness) {
        return depositbusinessDao.update(depositbusiness);
    }

    /**
     * 查询所有的订单数据
     *
     * @return 返回一个订单集合
     */
    @Override
    public List<Depositbusiness> findAll() {
        return depositbusinessDao.findAll();
    }

    /**
     * 分页查询
     *
     * @param start     开始行号
     * @param rows      每页行数
     * @param condition 查询条件
     * @return 集合
     */
    @Override
    public List<Depositbusiness> findByPage(int start, int rows, String condition) {
        int pages=((rows-1)*start)+1;
        return depositbusinessDao.findByPage(pages,start+pages,condition);
    }

    /**
     * 按条件统计出记录行数
     *
     * @param condition 条件
     * @return 记录行数
     */
    @Override
    public int count(String condition) {
        return depositbusinessDao.count(condition);
    }

    /**
     * 获取查询条件
     *
     * @param depositbusiness 对象
     * @return 条件字符串
     */
    @Override
    public String getcondition(Depositbusiness depositbusiness) {
        //定义一个空字符串用于以下条件不满足时返回
        String condition=" ";
        //判断depositbusiness不等于空
        if (depositbusiness==null){
            return condition;
        }
        //id	主键ID	int
        if(depositbusiness.getDeposiId()!=0){
            condition+="and deposiId like '%"+depositbusiness.getDeposiId()+"%'";
        }
        //depMoney	存款金额	number(16,2)
        if(depositbusiness.getDepMoney()!=0){
            condition+="and DepMoney like '%"+depositbusiness.getDepMoney()+"%'";
        }
        //businType	业务类型	varchar2
        if (depositbusiness.getBusinType()!=0){
            condition+="and businType in("+depositbusiness.getBusinType()+")";
        }
        //expiryDate	到期日期	date
        if(depositbusiness.getExpiryDate()!=null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            condition+="and expiryDate like to_date('"+sdf.format(depositbusiness.getExpiryDate())+"','yyyy-MM-dd')";
        }
        //incl	所含利息	number(16,2)
        if(depositbusiness.getIncl()!=0){
            condition+="and incl like '%"+depositbusiness.getIncl()+"%'";
        }
        //businTime	业务日期	date
        if(depositbusiness.getBusinTime()!=null){
            condition+="and businTime like '%"+depositbusiness.getBusinTime()+"%'";
        }
        //fundId	基金id	varchar2
        if (depositbusiness.getFundId()!=null && depositbusiness.getFundId().trim().length()!=0){
            condition+="and fundId like '%"+depositbusiness.getFundId()+"%'";
        }
        return condition;
    }

    /**
     * 库存统计后点击结算按钮添加应收应付
     * @param str
     */
    @Override
    public Boolean addCash(String str) {
        try {
            //设置库存集合
            ArrayList<Depositbusiness> list = new ArrayList<>();
            //从存款业务中通过id查询数据
            if (str.contains(",")){
                //分割字符串
                String[] strs=str.split(",");
                //循环遍历集合
                for (String s : strs) {
                    Depositbusiness dep = depositbusinessDao.queryDep(s);
                    //调用修改方法
                    depositbusinessDao.updateState(s);
                    //将dep添加到集合中
                    list.add(dep);
                }
            }else{
                Depositbusiness dep=depositbusinessDao.queryDep(str);
                //调用修改方法
                depositbusinessDao.updateState(str);
                //添加到集合
                list.add(dep);
            }
            //将存款业务的数据添加到现金应收应付
            for (Depositbusiness depositbusiness:list) {
                //int Id;//主键ID
                //Date businDate;//业务日期
                //获取现金账户编号
                String caId=depositbusiness.getCaId();
                //设置业务类型：现金利息，申购和赎回款，两费 businType
                String businType="存款";
                //设置资金流向 1：流入 -1：流出 capFlow
                int capFlow=-1;
                //获取金额
                double money=depositbusiness.getDepMoney();
                //获取基金id：
                String fundId=depositbusiness.getFundId();
                //设置统计状态 0：未统计，1：已统计 states
                int states=0;
                //添加到应收应付 addcashs
                depositbusinessDao.addCashs(caId,businType,capFlow,money,fundId,states);
                //设置调拨类型为存款业务 reType
                String reType="存款业务";
                //添加到资金调拨 dep
                depositbusinessDao.addDep(fundId,caId,reType,money,capFlow);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 取款
     * @param depositbusiness
     * @return
     */
    @Transactional
    @Override
    public Boolean getMoney(Depositbusiness depositbusiness) {
        try {
            //计算条件取出现金应收应付中该存款的现金利息和
            //通过现金账户，应收应付类型，现金金额进行判断：现金金额需要计算
            //首先要取得卡号利率、计算存款天数、和现金
            //通过流入账户取得卡号利率，并且计算出每天应该产生的利息
            Double cardRate = depositbusinessDao.getCardRate(Integer.parseInt(depositbusiness.getCaIds()));//获取账户的卡号：cardRate
            //计算存款的天数:dayCount 到期-开始/分秒时
            int dayCount =(int) (depositbusiness.getExpiryDate().getTime()-depositbusiness.getBusinTime().getTime()) / (60 * 60 * 24 * 1000);
            //计算每天的利息：rate ￥*利率/天
            Double rate=(depositbusiness.getDepMoney()*cardRate)/dayCount;
            //从现金应收应付中取出该存款中应收应付的利息 dueMoney
            Double dueMoney = depositbusinessDao.getDueMoney(depositbusiness.getCaIds(), rate);
            //删除应收应付中的数据
            depositbusinessDao.deleteDueMoney(depositbusiness.getCaIds(),rate);
            //添加资金调拨数据
            FundAllocation fundAllocation = new FundAllocation();
            //设置资金调拨的基金编号 fundId
            fundAllocation.setFundId(depositbusiness.getFundId());
            //设置资金调拨的现金账户编号 caId
            fundAllocation.setCaId(Integer.parseInt(depositbusiness.getCaId()));
            //设置资金调拨的调拨类型为存款到期 reType
            fundAllocation.setReType("存款到期");
            //设置资金调拨的调拨金额 reMoney(存款金额加存款利息)
            fundAllocation.setReMoney(depositbusiness.getDepMoney()+dueMoney);
            //设置资金调拨的业务日期 businDate
            fundAllocation.setBusinDate(new Date());
            //设置资金调拨的调拨日期 reDate
            fundAllocation.setReDate(new Date());
            //设置资金调拨的标记 mark
            fundAllocation.setMark("");
            //设置资金调拨的调拨方向为流入 dial
            fundAllocation.setDial(1);
            //调用方法添加资金调拨
            fundAllocationDao.add(fundAllocation);
            //删除这行存款业务
            depositbusinessDao.delete(depositbusiness.getDeposiId());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
